package com.suyuan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suyuan.dao.AntiepidemicPurcMapper;
import com.suyuan.dao.VaccineInforMapper;
import com.suyuan.dao.VaccoutstroeMapper;
import com.suyuan.entity.*;
import com.suyuan.service.VaccInStoreService;
import com.suyuan.service.VaccOutStoreService;
import com.suyuan.web.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service(value = "vaccOutStoreService")
public class VaccOutStoreServiceImpl implements VaccOutStoreService {
    @Resource
    private AntiepidemicPurcMapper antiepidemicPurcMapper;
    @Resource
    private VaccoutstroeMapper vaccoutstroeMapper;

    @Resource
    private VaccineInforMapper vaccineInforMapper;

    public PageInfo inquiry(Integer vaccineId, Date outDate, String remark, Page page) {
        VaccoutstroeExample vaccoutstroeExample=new VaccoutstroeExample();
        VaccoutstroeExample.Criteria criteria=vaccoutstroeExample.createCriteria();
        if(vaccineId!=null){
            criteria.andVaccineidEqualTo(vaccineId);
        }
        if(outDate!=null){
            criteria.andOutdateEqualTo(outDate);
        }
        if(remark!=null){
            criteria.andRemarkLike("%"+remark+"%");
        }
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<Vaccoutstroe> vaccoutstroeLis=vaccoutstroeMapper.selectByExample(vaccoutstroeExample);
        PageInfo info = new PageInfo(vaccoutstroeLis);
        return info;
    }

    /**
     * 保存疫苗出库信息
     * @param vaccoutstroe 疫苗出库信息
     * @param oldNumOutStore 原来的出库数量
     * @return
     */
    public int save(Vaccoutstroe vaccoutstroe,int oldNumOutStore){
        int a=vaccoutstroeMapper.insert(vaccoutstroe);
        if(a==0){
            return a;
        }
        int vaccineid=vaccoutstroe.getVaccineid();
        //若插入疫苗入出库信息表没出错，则修改疫苗信息表的库存量
        VaccineInfor vaccineInfor=vaccineInforMapper.selectByPrimaryKey(vaccoutstroe.getVaccineid());
        //库存量为原来的库存量-出库的数量
        vaccineInfor.setNuminstore(oldNumOutStore-vaccoutstroe.getNumout());
        vaccineInforMapper.updateByPrimaryKeySelective(vaccineInfor);
        return 1;//成功
    }

    /**
     * 删除疫苗出库信息
     * @param id 疫苗出库id
     * @return
     */
    public int delete(Integer id){
        Vaccoutstroe vaccoutstroe=vaccoutstroeMapper.selectByPrimaryKey(id);
        //删除时需要设置外键为空才能删除成功
        vaccoutstroe.setVaccineid(null);
        int a=vaccoutstroeMapper.updateByPrimaryKeySelective(vaccoutstroe);
        return vaccoutstroeMapper.deleteByPrimaryKey(id);
    }


    public  Vaccoutstroe findByOutStoreId(Integer outStoreId){
        VaccoutstroeExample vaccoutstroeExample=new VaccoutstroeExample();
        VaccoutstroeExample.Criteria criteria=vaccoutstroeExample.createCriteria();
        if(outStoreId!=null){
            criteria.andOutstoreidEqualTo(outStoreId);
            return vaccoutstroeMapper.selectByPrimaryKey(outStoreId);
        }
        return null;
    }

    /**
     * 修改疫苗出库信息
     * @param vaccoutstroe 疫苗出库信息
     * @param oldOutStoreNum 原来的疫苗出库数量
     * @return
     */
    public int update(Vaccoutstroe vaccoutstroe,Integer oldOutStoreNum){
        //更新疫苗出库信息之前首先要更新疫苗种类信息的库存量
        //获取前后库存量的差值(差值可能为负数,通过"新的出库数量-原来的出库数量"得出)
        int between=vaccoutstroe.getNumout()-oldOutStoreNum;
        VaccineInfor vaccineInfor=vaccineInforMapper.selectByPrimaryKey(vaccoutstroe.getVaccineid());
        //库存量为现在的库存量-差值
        vaccineInfor.setNuminstore(vaccineInfor.getNuminstore()-between);
        int code=vaccineInforMapper.updateByPrimaryKeySelective(vaccineInfor);
        //如果修改疫苗种类信息的库存量失败就返回不再继续执行
        if(code==0){
            return 0;
        }

        //最后更新修改后的疫苗出库信息
        vaccoutstroeMapper.updateByPrimaryKey(vaccoutstroe);
        return 1;
    }
}
